import java.text.DecimalFormat;
import java.util.*;
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Budgeting extends JFrame{
	
	private JLabel lblNetIncome;
	private JLabel lblFoodExp;
	private JLabel lblHouseExp;
	private JLabel lblTransportExp;
	private JLabel lblClothingExp;
	private JLabel lblRecreationExp;
	private JLabel lblCharityExp;
	private JLabel lblInsurancesExp;
	private JLabel lblLoanExp;
	private JLabel lblUtilityExp;
	private JLabel lblCommunicationExp;
	private JLabel lblRetirmentPlanExp;
	private JLabel lblOtherExp;
	private JLabel lblTotalExp;
	private JLabel lblSaving;
	
	private JTextField txtNetIncome;
	private JTextField txtFoodExp;
	private JTextField txtHouseExp;
	private JTextField txtTransportExp;
	private JTextField txtClothingExp;
	private JTextField txtRecreationExp;
	private JTextField txtCharityExp;
	private JTextField txtInsuranceExp;
	private JTextField txtLoanExp;
	private JTextField txtUtilityExp;
	private JTextField txtCommunicationExp;
	private JTextField txtRetirmentPlanExp;
	private JTextField txtOtherExp;
	private JTextField txtTotalExp;
	private JTextField txtSaving;
	
	private JButton btnTotalExp;
	private JButton btnSavings;
	private GridLayout gridLayout1;
	private Container container1;
	

	public Budgeting(){
		
		super("Family Badgeting");
		
		gridLayout1 = new GridLayout(16, 2);
		container1 = getContentPane();
		JPanel panel1 = new JPanel();
		container1.add(panel1, BorderLayout.CENTER);
		panel1.setLayout(gridLayout1);
		panel1.setBorder(BorderFactory.createTitledBorder("Enter Monthly Expenses"));
		
	
		lblNetIncome = new JLabel("Net Income:");
		panel1.add(lblNetIncome);
		txtNetIncome = new JTextField();
		panel1.add(txtNetIncome);
		lblFoodExp = new JLabel("Food Expenses:");
		panel1.add(lblFoodExp);
		txtFoodExp = new JTextField();
		panel1.add(txtFoodExp);
		lblHouseExp = new JLabel("House Expenses:");
		panel1.add(lblHouseExp);
		txtHouseExp = new JTextField();
		panel1.add(txtHouseExp);
		lblTransportExp = new JLabel("Transport Expenses:");
		panel1.add(lblTransportExp);
		txtTransportExp = new JTextField();
		panel1.add(txtTransportExp);
		lblClothingExp = new JLabel("Clothing Expenses");
		panel1.add(lblClothingExp);
		txtClothingExp = new JTextField();
		panel1.add(txtClothingExp);
		lblRecreationExp = new JLabel("Recreation Expenses");
		panel1.add(lblRecreationExp);
		txtRecreationExp = new JTextField();
		panel1.add(txtRecreationExp);
		lblCharityExp = new JLabel("Charity Expenses");
		panel1.add(lblCharityExp);
		txtCharityExp = new JTextField();
		panel1.add(txtCharityExp);
		lblInsurancesExp = new JLabel("Insurance Expenses");
		panel1.add(lblInsurancesExp);
		txtInsuranceExp = new JTextField();
		panel1.add(txtInsuranceExp);
		lblLoanExp = new JLabel("Loan Expenses");
		panel1.add(lblLoanExp);
		txtLoanExp = new JTextField();
		panel1.add(txtLoanExp);
		lblUtilityExp = new JLabel("Utility Expenses");
		panel1.add(lblUtilityExp);
		txtUtilityExp = new JTextField();
		panel1.add(txtUtilityExp);
		lblCommunicationExp = new JLabel("Communication Expenses");
		panel1.add(lblCommunicationExp);
		txtCommunicationExp = new JTextField();
		panel1.add(txtCommunicationExp);
		lblRetirmentPlanExp = new JLabel("Retirment Expenses");
		panel1.add(lblRetirmentPlanExp);
		txtRetirmentPlanExp = new JTextField();
		panel1.add(txtRetirmentPlanExp);
		lblOtherExp = new JLabel("Other Expenses");
		panel1.add(lblOtherExp);
		txtOtherExp = new JTextField();
		panel1.add(txtOtherExp);
		
		btnTotalExp = new JButton("Compute Total Expenses");
		panel1.add(btnTotalExp);
		
		btnSavings = new JButton("Compute Monthly Saving");
		panel1.add(btnSavings);
		
		lblTotalExp = new JLabel("Total Expenses");
		panel1.add(lblTotalExp);
		txtTotalExp = new JTextField();
		panel1.add(txtTotalExp);
		
		lblSaving  = new JLabel("Monthly Saving");
		panel1.add(lblSaving);
		txtSaving = new JTextField();
		panel1.add(txtSaving);
		
		BudgetHandler handler = new BudgetHandler();
		btnTotalExp.addActionListener(handler);
		btnSavings.addActionListener(handler);
		
	}
	
	public class BudgetHandler implements ActionListener {
		
		
		public void actionPerformed(ActionEvent e){
			
			double total;
			double sav;
			DecimalFormat decTot = new DecimalFormat("0.00");
			DecimalFormat decSav = new DecimalFormat("0.00");
			
			double nIncome = Double.parseDouble(txtNetIncome.getText());
			double fExp = Double.parseDouble(txtFoodExp.getText());
			double hExp = Double.parseDouble(txtHouseExp.getText());
			double tExp = Double.parseDouble(txtTransportExp.getText());
			double cExp = Double.parseDouble(txtClothingExp.getText());
			double rExp = Double.parseDouble(txtRecreationExp.getText());
			double chExp = Double.parseDouble(txtCharityExp.getText());
			double iExp = Double.parseDouble(txtInsuranceExp.getText());
			double lExp = Double.parseDouble(txtLoanExp.getText());
			double uExp = Double.parseDouble(txtUtilityExp.getText());
			double coExp = Double.parseDouble(txtCommunicationExp.getText());
			double rtExp = Double.parseDouble(txtRetirmentPlanExp.getText());
			double oExp = Double.parseDouble(txtOtherExp.getText());
			
			
			if(e.getSource() == btnTotalExp){
				
				total = fExp + hExp + tExp + cExp + rExp + chExp + iExp + lExp + uExp + coExp + rtExp + oExp;
				String mTotal = decTot.format(total);
				txtTotalExp.setText(mTotal);
			}
				
				if(e.getSource() == btnSavings){
					
					sav = nIncome - (fExp + hExp+ tExp + cExp + rExp + chExp + iExp + lExp + uExp + coExp + rtExp + oExp);
					String mSaving = decSav.format(sav);
					txtSaving.setText(mSaving);
					
					if(sav < 0){
						JOptionPane.showMessageDialog(null, "Over budget Expense");
												
					}
					else
						if(sav > 0 && sav <= 100){
							JOptionPane.showMessageDialog(null, "Not bad, try to save more");
						}
						else
							if(sav > 100 && sav <= 500){
								JOptionPane.showMessageDialog(null, "Good budgeting");
							}
							else
								if(sav > 500){
									JOptionPane.showMessageDialog(null, "Excellent, Keep it up");
								}
                        else
                          if(sav > 1000 && sav < 2000){
                          JOptionPane.showMessageDialog(null, "Unbelivable Saving");
                          }
                          else
                          {
                            JOptionPane.showMessageDialog(null, "Make sure you are not starving");
                          }
				}
			
		}
	}
	
	public static void main(String[] args) {
		Budgeting bdt = new Budgeting();
		bdt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		bdt.setSize(400, 560);
		bdt.setVisible(true);

	}

}
